var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // userInfro: {
        //     name: "Snow",
        //     gender: "男",
        //     mobile: "15399999999",
        //     card: "412728199607044823",
        //     drivers: "342221198307094978",

        // },
        avatar: "",
        gender: ['男', '女', ],
        genderI: 0, //性别索引
        contact: ["父亲", "母亲", "妻子", "伴侣", "自己", ],
        contactI: 0, //紧急联系人索引
        driversT: ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", ],
        driversI: 0, //驾驶证类型索引
        issuingDate: "", //发行日期
        dLID: "", //发行日期
        dLEDI: 0, //有效期索引
        dLEDA: ["6年", "10年", "长期"],
        modelShowC: true,
        confirm: "",
        identityZm: "", //身份证正面图片
        identityFm: "", //身份证反面图片
        driversZm: "", //驾驶证正面图片
        driversFm: "", //驾驶证反面图片
        dLN: "", //驾驶证号码
        iLN: "", //身份证号码
        eCT: "", //紧急联系人号码
        mobile: "", //手机号码
        lookM: true, //是否隐藏手机号码
        lookE: true, //是否隐藏紧急联系人手机号码
        lookI: true, //是否隐藏身份证号码
        lookD: true, //是否隐藏驾驶证号码
        save: false,
        focus: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function() {
        var that = this,
            dLEDI = 0;
        app.getApi.request("/api/Drivers/Detail?id=" + app.getApi.drivers.id, {}, "GET", (res) => {
            if (res.data.responseCode == 1000) {
                wx.hideLoading()
                that.setData({
                    avatar: res.data.obj.avatarUrl,
                    name: res.data.obj.name,
                    identityCardNumber: res.data.obj.identityCardNumber,
                    driverLicenseNumber: res.data.obj.driverLicenseNumber,
                    item: res.data.obj,
                    driveLicenseCertificationStatus: res.data.obj.driveLicenseCertificationStatus,
                    identityCertificationStatus: res.data.obj.identityCertificationStatus,
                    driveLicenseCertificationStatusName: res.data.obj.driveLicenseCertificationStatusName,
                    identityCertificationStatusName: res.data.obj.identityCertificationStatusName
                })
                app.getApi.drivers = res.data.obj
            }


        }, (res) => {})

        if (that.data.avatar ==''){
          wx.getUserInfo({
            success: function (res) {
              that.setData({
                avatar: res.userInfo.avatarUrl
              })
            }
          })
        }

    },
    bindfocus() {
        this.setData({
            save: true
        })
    },
    bindblur(e) {
        this.setData({
            save: false,
            name: e.detail.value
        })
    },
    save() {
        var that = this;
        var drivers = wx.getStorageSync("DRIVERS");
        app.getApi.request("/api/Drivers/UpdateNickName", { nickName: that.data.name, driverId: drivers.id }, "POST", (res) => {
            if (res.data.responseCode == 1000) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 2000
                });
            }


        }, (res) => {})

    },
    focus() {
        this.setData({
            focus: true
        })
    },
    upLoadImage() {
        var that = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths

                wx.uploadFile({
                    url: 'https://driverAPI.dwilms.com/api/Upload/UploadOnePic', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {},
                    success: function(res) {
                        var data = JSON.parse(res.data)
                        app.getApi.request("/api/Drivers/UpdateAvatar", { driverId: app.getApi.drivers.id, avatarUrl: data.obj }, "POST", (res) => {
                            if (res.data.responseCode == 1000) {
                                that.setData({
                                    avatar: data.obj
                                })
                                app.getApi.drivers.avatarUrl = data.obj
                            } else {
                                wx.showToast({
                                    title: '修改失败',
                                    icon: 'loading',
                                    duration: 2000
                                });
                            }

                        }, (res) => {});


                    },
                    fail: function(res) {
                        wx.showToast({
                            title: '修改失败',
                            icon: 'loading',
                            duration: 2000
                        });
                    }
                })
            },
            fail: function(res) {
                wx.showToast({
                    title: '上传失败',
                    icon: 'loading',
                    duration: 2000
                });
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    identity: function() {
        var item = JSON.stringify(this.data.item)
        if (this.data.identityCardNumber) {
            wx.navigateTo({
                url: "../identity/identity?item=" + item
            })
        }
    },
    drive: function() {
        var item = JSON.stringify(this.data.item)
        if (this.data.driverLicenseNumber) {
            wx.navigateTo({
                url: "../drive/drive?item=" + item
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})